




//
//  MBRegionSelectVC.m
//  发布求租
//
//  Created by MB__Lee on 2018/6/7.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBRegionSelectVC.h"

#import "MBNetTool.h"

#import "ArRegionModel.h"
#import "ArDistrictModel.h"

#import "MBQZRegionCell.h"
#import <MJExtension.h>


@interface MBRegionSelectVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *disTableview; // 地区
@property(nonatomic,strong)NSMutableArray <ArDistrictModel*>*disMut;  //地区数据源


@property(nonatomic,strong)UITableView *reTableview; // 区县
@property(nonatomic,strong)NSMutableArray <ArRegionModel *>*reMut; //区县数据源


@property(nonatomic,strong)NSIndexPath *disIndexPath;
@property(nonatomic,strong)NSIndexPath *reIndexPath;

//@property(nonatomic,strong)UIButton *testBTN;
@end
static NSString *MBQZRegionCell_Identi = @"MBQZRegionCell";
@implementation MBRegionSelectVC
//-(UIButton *)testBTN{
//    if (!_testBTN) {
//        _testBTN = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_testBTN addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//        _testBTN.frame = CGRectMake(0, 300, 200, 100);
//        _testBTN.backgroundColor = [UIColor greenColor];
//    }
//    return  _testBTN;
//}
-(void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(UITableView *)disTableview{
    if (!_disTableview) {
        UITableView *tableview  = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _disTableview = tableview;
        
        tableview.delegate = self;
        tableview.dataSource = self;
        [tableview registerNib:[UINib nibWithNibName:MBQZRegionCell_Identi bundle:nil] forCellReuseIdentifier:MBQZRegionCell_Identi];
        tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        tableview.tag = 100;
        
        _disMut = [NSMutableArray array];
        
    }
    return _disTableview;
}
-(UITableView *)reTableview{
    if (!_reTableview) {
        
        CGFloat x = self.view.bounds.size.width;
        CGFloat y = self.view.bounds.origin.y;
        CGFloat width = self.view.bounds.size.width;
        CGFloat hight = self.view.bounds.size.height;
        CGRect frame = CGRectMake(x, y, width, hight);
        
        UITableView *tableview  = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _reTableview = tableview;
        
        tableview.delegate = self;
        tableview.dataSource = self;
        
        tableview.backgroundColor = [UIColor clearColor];
        
        [tableview registerNib:[UINib nibWithNibName:MBQZRegionCell_Identi bundle:nil] forCellReuseIdentifier:MBQZRegionCell_Identi];
        tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableview.tag = 101;
        
        _reMut = [NSMutableArray array];
    }
    return _reTableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //1.初始化的一些设置
    self.title = @"选择区域";
    [self.view addSubview:self.disTableview];
    [self.view addSubview:self.reTableview];
    
    //2.获取网络数据
    [self MBNetTool_getSource];
    
    
//    [self.view addSubview:self.testBTN];
    
    
}
-(void)MBNetTool_getSource{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"city"] = MBCityCode;
    [MBNetTool MBPost:Place_district paramater:dict success:^(id response) {
        if ([response[@"code"] isEqualToString:@"10000"]) {
            NSMutableArray *mut = [ArDistrictModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            [mut insertObject:[self creatPHModel] atIndex:0];
            
            [_disMut addObjectsFromArray:mut];
            
            [self.disTableview reloadData];
            
        }else{
            NSLog(@"%@",response);
        }
    } failure:^(NSError *error) {
        NSLog(@"cuowu");
    }];
    
    
}
-(ArDistrictModel *)creatPHModel{ //生成占位模型用来显示 “不限”
    ArDistrictModel *model  = [[ArDistrictModel alloc] init];
    model.name = @"不限";
    return  model;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSInteger count;
    if (tableView.tag == 100) {
        count = _disMut.count;
    }else{
        count = _reMut.count;
    }
    return count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MBQZRegionCell *cell = [tableView dequeueReusableCellWithIdentifier:MBQZRegionCell_Identi];
    
    if (tableView.tag ==100) {
        if (_disMut.count) {
            cell.name = [_disMut[indexPath.row] name];
        }
    }else if (tableView.tag == 101){
        if (_reMut.count) {
            cell.name = [_reMut[indexPath.row] name];
        }
    }
    
    return  cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  50;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 100) {
        
        _disIndexPath = indexPath;
        
        if (indexPath.row == 0) {
            //点击 “不限” 直接返回
//            需要内容
            
            if ([self.delegate respondsToSelector:@selector(MBRegionSelectVCSendDistrictModel:regionModel:)]) {
                
                ArDistrictModel *disModel = _disMut[indexPath.row];
                [self.delegate MBRegionSelectVCSendDistrictModel:disModel regionModel:nil];
                
                [self.navigationController popViewControllerAnimated:YES];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
            
            return;
        }
        // 网络请求获取 region 数据
        [self getRegionSourceWithModel:_disMut[indexPath.row]];
        
    }else if (tableView.tag == 101){
        
        _reIndexPath = indexPath;
        // 点击region 数据的时候直接返回
        if ([self.delegate respondsToSelector:@selector(MBRegionSelectVCSendDistrictModel:regionModel:)]) {
            
            ArDistrictModel *disModel = _disMut[_disIndexPath.row];
            ArRegionModel *reModel = _reMut[indexPath.row];
            [self.delegate MBRegionSelectVCSendDistrictModel:disModel regionModel:reModel];
            
            [self.navigationController popViewControllerAnimated:YES];
            [self dismissViewControllerAnimated:YES completion:nil];
            
        }
        
        
    }
    
    
}
//获取 region 数据，并且移动 reTableview 的位置
-(void)getRegionSourceWithModel:(ArDistrictModel*)model{
    
    if (model.regionMut.count) {
        [_reMut removeAllObjects];
        [_reMut addObjectsFromArray:model.regionMut];
        [_reTableview reloadData];
        return;
    }
    
    
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"city"] = MBCityCode;
    dic[@"district"] = model.code;
    
    [MBNetTool MBPost:Place_region paramater:dic success:^(id response) {
        
        
        if ([response[@"code"] isEqualToString:@"10000"]) {
            NSMutableArray *mut = [ArRegionModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            [mut insertObject:[self creatARregionPHModel] atIndex:0];
            model.regionMut = mut;
            [_reMut removeAllObjects];
            [_reMut addObjectsFromArray:mut];
            
            
            
            [self setNewFrameToReTableview];
            [self.reTableview reloadData];
            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.reTableview scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES];
            
        }else{
            
        }
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
-(ArRegionModel *)creatARregionPHModel{// 生成占位模型
    ArRegionModel *model  = [[ArRegionModel alloc] init];
    model.name = @"不限";
    return model;
}
-(void)setNewFrameToReTableview{
    
    //1.调整 region frame
    CGRect frame = self.reTableview.frame;
    frame.origin.x = self.view.bounds.size.width * 0.5;
    self.reTableview.frame = frame;
    
    //2.调整 district frame
    CGRect diFrame = self.disTableview.frame;
    diFrame.size.width = self.view.bounds.size.width * 0.5;
    self.disTableview.frame =  diFrame;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
